﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Epam.Exceptions.BusinessLogicExceptions;
using Epam.PeopleInterfaces;
using Microsoft.Practices.Unity;

namespace PeopleListTask.Controllers
{
    public class PeoplesController : Controller
    {
        private readonly IPeopleService _peopleService;

        private readonly IJsonPeopleService _jsonPeopleService;

        public PeoplesController(IPeopleService peopleService,IJsonPeopleService jsonPeopleService)
        {
            _peopleService = peopleService;
            _jsonPeopleService = jsonPeopleService;
        }

        [HttpGet]
        public ActionResult Index()
        {
            try
            {
               return View();
            }
            catch (DatabaseException e)
            {
                ViewData["errorMessage"] = e.Message;
                return View("Error");
            }
           
        }
        
        public ActionResult GetViewPeopleList()
        {
            return Json(_jsonPeopleService.PeopleConvert(_peopleService.GetAllViewEntity()), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Index(double? minFactor,double? maxFactor)
        {
            try
            {
                
                if (minFactor != null && maxFactor != null)
                {
                    var list = _peopleService.Filter(minFactor.Value, maxFactor.Value,
                                                     DependencyManager.Container.Resolve<IFilterAlgoritm>());
                    return View(list);
                }
                return View(new List<IViewPeopleEntity>());
            }
            catch (DatabaseException e)
            {
                ViewData["errorMessage"] = e.Message;
                return View("Error");
            }
           
        }

        [HttpPost]
        public ActionResult Create(IEnumerable<IPeopleEntity> entities)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    foreach (var peopleEntity in entities)
                    {
                        _peopleService.Create(peopleEntity);
                    }
                    return Json(new { success = true }, JsonRequestBehavior.AllowGet);
                }
                return Json(new { success = false }, JsonRequestBehavior.AllowGet);
            }
            catch (DatabaseException)
            {
                return Json(new { success = false }, JsonRequestBehavior.AllowGet);
            }
         
        }

        public ActionResult AdminList()
        {
            try
            {
                return View();
            }
            catch (DatabaseException e)
            {
                ViewData["errorMessage"] = e.Message;
                return View("Error");
            }
            
        }

        public ActionResult GetAdminJsonList()
        {
            var jsonlist = _jsonPeopleService.PeopleConvert(_peopleService.GetAll());
            return Json(jsonlist, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Delete(IEnumerable<Guid> root)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    foreach (var item in root)
                    {
                        _peopleService.Delete(item);
                    }
                    return Json(new { success = true }, JsonRequestBehavior.AllowGet);
                }
                return Json(new { success = false }, JsonRequestBehavior.AllowGet);
            }
            catch (DatabaseException)
            {
                return Json(new { success = false }, JsonRequestBehavior.AllowGet);
            }
         
        }

        [HttpPost]
        public ActionResult Update(IEnumerable<IPeopleEntity> root)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    foreach(var item in root)
                    {
                        _peopleService.Update(item);
                    }
                    return Json(new { success = true }, JsonRequestBehavior.AllowGet);
                }
                return Json(new { success = false }, JsonRequestBehavior.AllowGet);
            }
            catch (DatabaseException)
            {
                return Json(new { success = false }, JsonRequestBehavior.AllowGet);
            }
          
        }


    }
}
